<?php
/**
 *
 * The model for the user data
 * @author Simon,Dennis,Denis
 *
 */
class User_model extends CI_Model {

	/**
	 *
	 * Constructor
	 */
	function User_model()
	{
		$this->load->database();

	}

	/**
	 * get top10 users
	 */
	function getTopUsers()
	{
		$this->db->order_by("Scalepoints", "desc");
		$this->db->limit(10);
		$query = $this->db->get('users');
		return $query->result();
	}

	/**
	 * method to return all users and their entrys
	 */
	function getAllUsers()
	{
		//Query the data table for every record and row
		$query = $this->db->get('users');
			
		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->result();
		}
	}
	/**
	 * method to return the user by the user ID
	 * @param int $id
	 */
	function getUserByID($id)
	{
		$query = $this->db->get_where('users', array('ID' => $id));

		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->row();
		}
	}

	/**
	 * method to return the user by the username
	 * @param string $username
	 */
	function getUserByUsername($username)
	{
		$query = $this->db->get_where('users', array('username' => $username));
			
		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->row();
		}
	}
	
	
	/**
	 * checks if the username already exists in the DB
	 * @param string $username
	 */
	function checkIfUsernameExist($username)
	{
		$query = $this->db->get_where('users', array('username' => $username));
			
		if ($query->num_rows() < 1)
		{
			return false;
		}else{
			return true;
		}
	}

/**
 * 
 * checks if the password matches to the username
 * @param string $username
 * @param string $password
 */
	function checkUserAndPassword($username,$password)
	{
		$query = $this->db->get_where('users', array('username' => $username, 'password' =>$password));
		echo $query->num_rows();
		if ($query->num_rows() < 1)
		{
			return FALSE;
		}else{
			return TRUE;
		}
	}
	/**
	 * method to add an user. returns true if the transaction was successfull
	 * @param array $userarray array with user data
	 */
	function addUser($username,$password,$mail,$ip,$status,$surname,$name,$pro,$size,$weight,$gender,$city,$country,$birthday)
	{
		$data = array(
		    'username' => $username,
		    'password' =>  sha1( md5( $password ) ) ,
		    'email' => $mail,
			'ip' => $ip,
			'status' => $status,
			'Surname' => $surname,
			'Name' => $name,
			'Pro' => $pro,
			'Size' => $size,
			'Weight' => $weight,
			'Gender' => $gender,
			'City' => $city,
			'Country' => $country,
			'Birthday' => $birthday
		);

		$this->db->insert('users', $data);

	}


	
	/**
	*
	* method to modify an user. returns true if the transaction was successfull
	* @param $id
	* @param $username
	* @param $password
	* @param $mail
	* @param $surname
	* @param $name
	* @param $scalepoints
	* @param $pro
	* @param $size
	* @param $weight
	* @param $gender
	* @param $city
	* @param $country
	* @param $birthday
	*/
	function modifyUser($id, $surname, $name, $pro, $size, $weight, $gender, $city, $country, $birthday)
	{
		$data = array(
				    'username' => $username,
					'Surname' => $surname,
					'Name' => $name,
					'Pro' => $pro,
					'Size' => $size,
					'Weight' => $weight,
					'Gender' => $gender,
					'City' => $city,
					'Country' => $country,
					'Birthday' => $birthday
		);
		$this->db->where('id', $id);
		return $this->db->update('users', $data);
	}
	
	/**
	 * 
	 * method to update the scalepoints of one user
	 * @param int $id
	 * @param int $scalepoints
	 */
	function updateScalepoints($id,$scalepoints){
		$data = array(
		               'Scalepoints' => $scalepoints
		);
		
		$this->db->where('id', $id);
		return $this->db->update('users', $data);
	}

	/**
	 * deletes a user by the user id
	 * @param int $id user id
	 */
	function deleteUser($id)
	{
		return $this->db->delete('users', array('ID' => $id));
	}


}

?>